package com.mlt.back.dao.impl;

import com.mlt.back.beans.InfoDetail;
import com.mlt.back.dao.InfoDetailDao;
import com.mlt.back.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * 数据库操作
 *@author 李
 *@date 2022/12/28
*/

public class InfoDetailDaoImpl implements InfoDetailDao {

    @Override
    public int insert(InfoDetail infoDetail) {

        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn= DBUtil.getConnection();
            String sql="insert into t_info_detail(info_detail_name,info_detail_desc,fk_basic_info_id) values(?,?,?)";
            ps=conn.prepareStatement(sql);
            ps.setString(1, infoDetail.getInfoDetailName());
            ps.setString(2, infoDetail.getInfoDetailDesc());
            ps.setInt(3, infoDetail.getFkBasicInfoId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,null);
        }
        return 0;

    }

    @Override
    public int delete(Integer id) {

        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn= DBUtil.getConnection();
            String sql="delete from t_info_detail where info_detail_id=?";
            ps=conn.prepareStatement(sql);
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,null);
        }
        return 0;

    }

    @Override
    public int update(InfoDetail infoDetail) {

        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn= DBUtil.getConnection();
            String sql="update t_info_detail set info_detail_name=?,info_detail_desc=?,fk_basic_info_id=? where info_detail_id=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1, infoDetail.getInfoDetailName());
            ps.setString(2, infoDetail.getInfoDetailDesc());
            ps.setInt(3, infoDetail.getFkBasicInfoId());
            ps.setInt(4, infoDetail.getInfoDetailId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,null);
        }
        return 0;

    }

    @Override
    public List<InfoDetail> selectAll() {

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn= DBUtil.getConnection();
            String sql="select * from t_info_detail";
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();

            InfoDetail infoDetail = null;
            List<InfoDetail> infoDetails = new ArrayList<>();
            while (rs.next()){

                infoDetail = new InfoDetail();
                infoDetail.setInfoDetailId(rs.getInt("info_detail_id"));
                infoDetail.setInfoDetailName(rs.getString("info_detail_name"));
                infoDetail.setInfoDetailDesc(rs.getString("info_detail_desc"));
                infoDetail.setFkBasicInfoId(rs.getInt("fk_basic_info_id"));

                infoDetails.add(infoDetail);

            }
            return infoDetails;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return null;

    }

}
